package com.iot.ic.service;



import com.iot.ic.common.CustomException;
import com.iot.ic.common.Options;
import com.iot.ic.common.ReturnCode;
import com.iot.ic.dao.StadiumResultMapper;
import com.iot.ic.dao.StadiumScenesMapper;
import com.iot.ic.dao.TeamInfoMapper;
import com.iot.ic.model.MatchScenesInfo;
import com.iot.ic.model.StadiumInfo;
import com.iot.ic.model.StadiumScenes;
import com.iot.ic.model.TeamInfo;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * Created by xiuge on 2017/2/20.
 */
@Service
public class StadiumScenesServiceImpl implements StadiumScenesService {
	
    @Autowired
    private StadiumScenesMapper stadiumScenesMapper;
    
    @Autowired
    private MatchScenesService matchScenesService;

	@Override
	public int bindStadiumScenes(String scenesIds, int stadiumId) {	
		

        stadiumScenesMapper.deleteByStadiumId(stadiumId);
        
		int flag = 0;
		int scenesNo = 1;
		String[] scenesIdArray = scenesIds.split(",");
		for (int i = 0; i< scenesIdArray.length; i++) {
			int scenesId = Integer.parseInt(scenesIdArray[i]);
			StadiumScenes stadiumScenes = new StadiumScenes();
			stadiumScenes.setStadiumId(stadiumId);
			stadiumScenes.setScenesId(scenesId);
			stadiumScenes.setCreateTime(new Date());
			stadiumScenes.setDelFlag(Options.NotDel.getCode());
			stadiumScenes.setSort(i+1);
			MatchScenesInfo matchScenesInfo = matchScenesService.getMatchScenesById(scenesId);
			if (matchScenesInfo.getScenesType() == Options.Scenes.getCode()) {
				stadiumScenes.setScenesNo(scenesNo);
				scenesNo ++;
			}
			flag =stadiumScenesMapper.insert(stadiumScenes);
		}
		return flag;
	}

	@Override
	public int sortScenes(String scenesIds) {
		int flag = 0;
		String[] scenesIdArray = scenesIds.split(",");
		for (int i = 0; i< scenesIdArray.length; i++) {
			int id = Integer.parseInt(scenesIdArray[i]);
			StadiumScenes stadiumScenes = new StadiumScenes();
			stadiumScenes.setStadiumScenesId(id);
			stadiumScenes.setSort(i);
			flag = stadiumScenesMapper.updateByPrimaryKeySelective(stadiumScenes);
		}
		return flag;
	}

	@Override
	public int unboundMatchScenes(String stadiumScenesIds) {
		int flag = 0;
		for (String stadiumScenesId : stadiumScenesIds.split(",")) {
			flag = stadiumScenesMapper.deleteByPrimaryKey(Integer.parseInt(stadiumScenesId));
		}
		return flag;
	}

	@Override
	public List<Integer> listStadiumScenesIdsByStadium(int stadiumId) {
		List<Integer> stadiumScenesIdList = stadiumScenesMapper.listStadiumScenesIdsByStadium(stadiumId);
		return stadiumScenesIdList;
	}

	@Override
	public List<StadiumScenes> listStadiumScenesByStadium(int stadiumId) {
		List<StadiumScenes> stadiumScenesList = stadiumScenesMapper.listStadiumScenesByStadium(stadiumId);
		return stadiumScenesList;
	}

	@Override
	public List<StadiumScenes> listStadiumScenesByScenesId(int scenesId) {
		List<StadiumScenes> stadiumScenesList = stadiumScenesMapper.listStadiumScenesByScenes(scenesId);
		return stadiumScenesList;
	}





	
}
